/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.boot;


import com.jspx.boot.environment.Environment;
import com.jspx.boot.environment.impl.BaseConfigurationImpl;
import com.jspx.boot.environment.impl.EnvironmentTemplateImpl;
import com.jspx.boot.environment.impl.PlaceholderImpl;
import com.jspx.boot.environment.JspxConfiguration;
import com.jspx.boot.environment.EnvironmentTemplate;
import com.jspx.boot.environment.Placeholder;
import com.jspx.security.asymmetric.AsyEncrypt;
import com.jspx.security.asymmetric.impl.RSAEncrypt;
import com.jspx.security.symmetry.Encrypt;
import com.jspx.security.symmetry.impl.SM4Encrypt;
import com.jspx.sioc.factory.EntryFactory;
import com.jspx.sioc.BeanFactory;
import com.jspx.utils.ClassUtil;
import com.jspx.utils.StringUtil;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2006-11-7
 * Time: 10:20:34
 *
 */

public abstract class EnvFactory
{
    private EnvFactory()
    {

    }

    /**
     * 最基本的配置，得到默认路径
     */
    static private JspxConfiguration jspxConfiguration = new BaseConfigurationImpl();
    public static JspxConfiguration getBaseConfiguration()
    {
        return jspxConfiguration;
    }

    /**
     * 提供载入文件名
     * @param configuration 配置
     */
    public static void setJspxConfiguration(JspxConfiguration configuration) {
        jspxConfiguration = configuration;
    }

    /**
     * SIoc 上下文
     */
    static private BeanFactory beanFactory = new EntryFactory();
    static public BeanFactory getBeanFactory()
    {
        return beanFactory;
    }

    static private EnvironmentTemplate envTemplate = new EnvironmentTemplateImpl();

    /**
     * 环境变量和环境模版
     * @return 得到环境变量
     */
    static public EnvironmentTemplate getEnvironmentTemplate()
    {
        return envTemplate;
    }

    static private Placeholder placeholder = new PlaceholderImpl();
    static public Placeholder getPlaceholder()
    {
        return placeholder;
    }


    static public String getHashAlgorithm()
    {
        return envTemplate.getString(Environment.hashAlgorithm,"Md5");
    }

    static public String getHashAlgorithmKey()
    {
        return envTemplate.getString(Environment.hashAlgorithmKey,"");
    }

    //对称加密实例
    static private Encrypt symmetryEncrypt = null;
    static public Encrypt getSymmetryEncrypt()
    {
        if (symmetryEncrypt!=null) return symmetryEncrypt;
        String encryptionAlgorithmClass = envTemplate.getString(Environment.symmetryAlgorithm, SM4Encrypt.class.getName());
        try {
            symmetryEncrypt = (Encrypt) ClassUtil.newInstance(encryptionAlgorithmClass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String secretKey = envTemplate.getString(Environment.secretKey, Environment.defaultDrug);
        if (secretKey.length() > 16) secretKey = StringUtil.cut(secretKey, 16, StringUtil.empty);
        symmetryEncrypt.setSecretKey(secretKey);
        symmetryEncrypt.setCipherAlgorithm(envTemplate.getString(Environment.cipherAlgorithm));
        symmetryEncrypt.setCipherIv(envTemplate.getString(Environment.cipherIv));
        return symmetryEncrypt;
    }


    /**
     * 非对称加密算法
     */
    static private AsyEncrypt asymmetricEncrypt = null;
    static public AsyEncrypt getAsymmetricEncrypt()
    {
        if (asymmetricEncrypt!=null) return asymmetricEncrypt;
        String encryptionAlgorithmClass = envTemplate.getString(Environment.asymmetricAlgorithm, RSAEncrypt.class.getName());
        try {
            asymmetricEncrypt = (AsyEncrypt) ClassUtil.newInstance(encryptionAlgorithmClass);
        } catch (Exception e) {
            e.printStackTrace();
            return new RSAEncrypt();
        }
        return asymmetricEncrypt;
    }

    static public String getPrivateKey()
    {
        return StringUtil.trim(envTemplate.getString(Environment.privateKey));
    }

    static public String getPublicKey()
    {
        return StringUtil.trim(envTemplate.getString(Environment.publicKey));
    }

}